<template>
    <div class="my">
        <div class="user-info" :style="{ backgroundImage: `url(${userBg})` }">
            <div class="user-top">
                <span class="login" @click="toLogin">{{ uselogining.isLogining==1 ? '退出登陆' : '立即登录' }}</span>
            </div>
            <div class="user-center">
                <div class="user-img">
                    <img :src="uselogining.isLogining==1?userImg:''">
                    <p>用户头像</p>
                </div>
                <div class="user-maessge">
                    <div class="user-name" v-if="uselogining.isLogining==0">
                        <h3 class="name">用户名</h3>
                        <span class="grade">lv1</span>
                    </div>
                    <div class="user-name" v-for="(item, index) in uselogining.userinfoData" :key="index"  v-else>
                        <h3 class="name">{{ item.username }}</h3>
                        <span class="grade">lv3</span>
                    </div>
                    <div class="user-detail">
                        <span>
                           {{uselogining.isLogining==1?'厨娘':'新手'}} 
                        </span>
                        <span>
                            {{uselogining.isLogining==1?'豆龄2岁':'豆龄0岁 '}} 
                        </span>
                        <span>
                            {{uselogining.isLogining==1?'IP属地 广东省':'IP属地 未知'}}
                        </span>
                    </div>
                </div>
            </div>
            <div class="user-bottom">
                <div class="user-item" v-for="(item, index) in userItemData" :key="index">
                    <h3>{{uselogining.isLogining==1? item.acount:"0" }}</h3>
                    <p>{{ item.name }}</p>
                </div>
            </div>
            <div class="user-vip">
                <img src="../assets/vip.png" alt="">
            </div>
        </div>
        <div class="user-channel">
            <div class="user-content" :class="{ active: isShow }">
                <div class="channel-item" v-for="(item, index) in userChannelData" :key="index">
                    <img :src="item.icon">
                    <p>{{ item.title }}</p>
                </div>
            </div>
            <div class="show">
                <span @click="show">{{ isShow ? '收起' : '展开' }}</span>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { getConfig } from '@/api/api';
import { onMounted, ref, reactive } from 'vue';
import { useLogin, useLogining } from "../stores/index"
let userBg: any = ref<string>("")
let userChannelData: any = ref<Array<any>>([])
let userItemData: any = reactive<Array<any>>([
    {
        acount: 10,
        name: "关注"
    },
    {
        acount: 20,
        name: "粉丝"
    },
    {
        acount: 30,
        name: "被收藏"
    },
    {
        acount: 40,
        name: "被学作"
    },
    {
        acount: 50,
        name: "经验值"
    }
])
let isShow: any = ref<boolean>(false)
let uselogin: any = useLogin()
let uselogining: any = useLogining()
let userImg:any=ref<string>(require("../assets/touxiang.webp"))
let show: () => void = (): void => {
    isShow.value = !isShow.value
}
//进入登录页面
let toLogin: () => void = (): void => {
    if (uselogining.isLogining == 0) {
        uselogin.updateLogin(true)
    } else {
        uselogining.updateLogining(0)
        localStorage.setItem("isLogin",JSON.stringify( uselogining.isLogining ))
    }
}
onMounted((): void => {
    //获取全局数据
    getConfig().then((res: any) => {
        console.log(res)
        userBg.value = res.data.result.user_head_cover
        userChannelData.value = res.data.result.user_channels
    }).catch((err: any) => {
        console.log(err)
    })
    //获取本地登录状态,登录为isLogin=1,否者为0
    let useLogining = localStorage.getItem("isLogin")
    if (useLogining) {
        uselogining.updateLogining(useLogining)
       let uinfoData = localStorage.getItem("userinfoData")
        //获取本地保存的用户数据
        if (uinfoData) {
            uinfoData= JSON.parse(uinfoData)
            uselogining.updateuserinfoData(uinfoData)
        }
    }
})
</script>

<style scoped lang="scss">
.my {
    .user-info {
        padding: 10px;
        background-repeat: no-repeat;
        background-size: cover;
        background-position: center center;
        height: 250px;
        border-bottom-right-radius: 50px;
        border-bottom-left-radius: 50px;
        box-sizing: border-box;

        .user-top {
            text-align: right;

            .login {
                font-size: 16px;
                font-weight: bold;
            }
        }

        .user-center {
            display: flex;
            align-items: center;
            margin-top: 20px;

            .user-img {
                width: 80px;
                height: 80px;
                background: rgba(255, 255, 255, .5);
                border-radius: 50%;
                margin-right: 10px;
                overflow: hidden;
                img{
                    width: 100%;
                }

                p {
                    text-align: center;
                    font-size: 14px;
                    color: #777;
                }
            }

            .user-name {
                display: flex;
                align-items: center;
                font-size: 14px;

                .name {
                    font-size: 18px;
                    margin: 0;
                    margin-right: 10px;
                }

                .grade {
                    background: #f9c653;
                    padding: 2px 10px;
                    border-radius: 10px;
                    margin-top: 4px;
                }

            }

            .user-detail {
                margin-top: 10px;

                span {
                    margin-right: 10px;
                    font-size: 14px;
                    font-weight: bold;
                }
            }
        }

        .user-bottom {
            display: flex;
            justify-content: space-around;
            text-align: center;

            h3 {
                font-size: 18px;
                font-weight: bold;
                margin-bottom: 0;
            }

            p {
                margin-top: 5px;
                font-size: 14px;
                color: #777;
            }
        }

        .user-vip {
            text-align: center;
        }
    }

    .user-channel {
        margin-top: 40px;
        padding: 10px 15px;

        .user-content {
            display: flex;
            flex-wrap: wrap;
            height: 140px;
            overflow: hidden;
            transition: height .25s;

            &.active {
                height: 200px;
            }

            .channel-item {
                text-align: center;
                width: 20%;

                img {
                    width: 40px;
                    height: 40px;
                    vertical-align: bottom;
                }

                p {
                    margin: 5px 0;
                    font-weight: bold;
                }
            }
        }

        .show {
            text-align: right;
            padding: 0 20px;
            font-size: 13px;
            color: #777;
        }
    }
}</style>